package com.app.micaihu.view.main.game.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.base.BaseOldActivity;
import com.app.micaihu.bean.game.GameCenterEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.UmEventId;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.custom.view.RatingBarView;
import com.app.micaihu.utils.MobclickAgentUtils;
import com.app.micaihu.view.main.game.GameDetailActivity;
import com.app.micaihu.view.main.game.GameRankingActivity;
import com.app.micaihu.view.main.game.view.RanklGameView;
import com.app.utils.util.StringUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.baidu.mobstat.StatService;
import com.blankj.utilcode.util.Utils;

import java.util.List;

/**
 * 游戏中心适配器
 * Created by linkai on 2017/3/13.
 */
public class GameCenterAdapter extends AppBaseAdapter<GameCenterEntity> implements View.OnClickListener {


    public GameCenterAdapter(List<GameCenterEntity> list, Context context) {
        super(list, context);
    }

    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {

        GameCenterEntity gameCenterEntity = list.get(position);
        if (gameCenterEntity == null) {
            gameCenterEntity = new GameCenterEntity();
            gameCenterEntity.setIsRankList("0");
            //适配隐藏样式
        }

        int type = getItemViewType(position);
        if (convertView == null) {
            switch (type) {
                case 0:
                    convertView = inflater.inflate(R.layout.item_layout_gone, null);
                    break;
                case 1:
                    convertView = inflater.inflate(R.layout.item_game_center, null);
                    convertView.setBackgroundResource(R.drawable.item_newslist_bg_selector);
                    ViewHolder viewHolder = new ViewHolder(convertView);
                    viewHolder.loadData(gameCenterEntity);
                    convertView.setTag(viewHolder);
                    break;
                case 2:
                    convertView = inflater.inflate(R.layout.layout_game_center_ranklist, null);
                    ViewHolder1 viewHolder1 = new ViewHolder1(convertView);
                    viewHolder1.loaddata(gameCenterEntity);
                    convertView.setTag(viewHolder1);
                    break;
            }
        } else {
            switch (type) {
                case 0:
                    convertView = inflater.inflate(R.layout.item_layout_gone, null);
                    break;
                case 1:
                    //如果为空字符串则是游戏样式
                    convertView.setBackgroundResource(R.drawable.item_newslist_bg_selector);
                    ViewHolder viewHolder = (ViewHolder) convertView.getTag();
                    viewHolder.loadData(gameCenterEntity);
                    break;
                case 2:
                    ViewHolder1 viewHolder1 = (ViewHolder1) convertView.getTag();
                    viewHolder1.loaddata(gameCenterEntity);
                    break;
            }
        }

        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        GameCenterEntity gameCenterEntity = list.get(position);
        if (gameCenterEntity == null) {
            return 0;
        } else return gameCenterEntity.getType();
//        if (TextUtils.isEmpty(gameCenterEntity.getIsRankList())) {
////            MobclickAgentUtils.event(Config.EventBauduId.game_exposed, "游戏");
//            MobclickAgentUtils.event(UmEventId.game_exposed, UmEventId.INSTANCE.getList());
//            StatService.onEvent(Utils.getApp().getApplicationContext(), "game_exposed", "游戏", 1);//统计事件的点击次数
//            return 1;
//        } else if (TextUtils.equals(gameCenterEntity.getIsRankList(), "0") && rankList != null && rankList.size() > 0) {
//            return 2;
//        }
//        return 0;
    }

    @Override
    public void onClick(View v) {
        if (v == null || context == null) {
            return;
        }
        if (v.getId() == R.id.rank_more || v.getId() == R.id.rank_list) {
            Intent intent1 = new Intent();
            intent1.setClass(context, GameRankingActivity.class);
            context.startActivity(intent1);
            StatService.onEvent(AppApplication.getInstance(), "061", "点击更多跳转排行榜", 1);
        } else {
            if (v.getTag() == null) {
                return;
            }
            Intent intent = new Intent();
            intent.putExtra(Config.Extra.PARAMETER1, v.getTag().toString());
            intent.setClass(context, GameDetailActivity.class);
            context.startActivity(intent);
//            if (!TextUtils.isEmpty(gameCenterEntity.getArticleId())) {
//                AdConfig.staticsAd(gameCenterEntity.getArticleId(), type);
//            }
//            GameDownloadActivity.insertGame(gameCenterEntity.getArticleId(), gameCenterEntity.getArticleTitle(), gameCenterEntity.getDownloadUrl(),
//                    gameCenterEntity.getAppIcon(), gameCenterEntity.getArticleSummary(), gameCenterEntity.getPackageName(), gameCenterEntity.getIPath());
        }
    }

    private class ViewHolder {
        CustomImageView headView;
        TextView sourceView;
        CustomImageView bigImgView;
        TextView commentView;
        TextView titleView;
        TextView descView;
//        DownProgressView downloadView;

        TextView gameScore;
        RatingBarView ratingBarView;


        public ViewHolder(View contentView) {
            headView = (CustomImageView) contentView.findViewById(R.id.game_logo);
            sourceView = (TextView) contentView.findViewById(R.id.game_source);
            bigImgView = (CustomImageView) contentView.findViewById(R.id.game_icon);
            commentView = (TextView) contentView.findViewById(R.id.game_comment);
            titleView = (TextView) contentView.findViewById(R.id.game_title);
            descView = (TextView) contentView.findViewById(R.id.game_desc);

            gameScore = (TextView) contentView.findViewById(R.id.game_score);
            ratingBarView = (RatingBarView) contentView.findViewById(R.id.rb_score);
//            downloadView = (DownProgressView) contentView.findViewById(R.id.game_download);
//            downloadView.setOnClickListener(GameCenterAdapter.this);
        }

        public void loadData(GameCenterEntity gameCenterEntity) {
            LoadImageUtils.getInstance().loadListCircleImage(headView, gameCenterEntity.getSourceIcon());
            if (gameCenterEntity.getThumbList() != null && gameCenterEntity.getThumbList().size() > 0) {
                LoadImageUtils.getInstance().loadImage(bigImgView, gameCenterEntity.getThumbList().get(0));
            }
            titleView.setText(gameCenterEntity.getArticleTitle());
            sourceView.setText(gameCenterEntity.getSourceType());
            if (TextUtils.isEmpty(gameCenterEntity.getSlogan())) {
                descView.setVisibility(View.GONE);
            } else {
                descView.setText(gameCenterEntity.getSlogan());
                if (descView.getVisibility() == View.GONE) {
                    descView.setVisibility(View.VISIBLE);
                }
            }

            ratingBarView.setStar(StringUtils.toFloat(gameCenterEntity.getGradeScore(), 0.0f) / 2);
            gameScore.setText(gameCenterEntity.getGradeScore());


            commentView.setText(gameCenterEntity.getCommentNum() + "评论");
//            downloadView.setTag(gameCenterEntity);
        }

    }

    private class ViewHolder1 {
        LinearLayout gameLayout;
        TextView tvTitle;

        public ViewHolder1(View contentView) {
            gameLayout = (LinearLayout) contentView.findViewById(R.id.ll_gamelayout);
            tvTitle = contentView.findViewById(R.id.rank_list);
            contentView.findViewById(R.id.rank_more).setOnClickListener(GameCenterAdapter.this);
            contentView.findViewById(R.id.rank_list).setOnClickListener(GameCenterAdapter.this);
        }

        public void loaddata(GameCenterEntity game) {
            List<GameCenterEntity> rankList = game.getRankList();
            if (rankList != null && rankList.size() > 0) {
                tvTitle.setText(game.getTitle());
                if (gameLayout.getVisibility() == View.GONE) {
                    gameLayout.setVisibility(View.VISIBLE);
                    for (int i = 0; i < rankList.size(); i++) {
                        GameCenterEntity gameCenterEntity = rankList.get(i);
                        RanklGameView ranklGameView = new RanklGameView(context, gameCenterEntity, i);
                        ranklGameView.setTag(gameCenterEntity.getArticleId());
                        ranklGameView.setOnClickListener(GameCenterAdapter.this);
                        gameLayout.addView(ranklGameView);
                    }
                }
            } else if (gameLayout.getVisibility() == View.VISIBLE) {
                gameLayout.setVisibility(View.GONE);
            }
        }
    }


}
